package com.kyzh.core.dialog

import android.content.Context
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import android.widget.EditText
import android.widget.ImageView
import android.widget.RatingBar
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import com.gushenge.core.requests.VideoRequest
import com.kyzh.core.R
import com.kyzh.core.utils.setVisibility
import com.gushenge.core.toast


private var dialog:AlertDialog?=null

fun Context.showVideoDiscussAddDialog(id: String,isVideo:Boolean,listener:()->Unit){
    dialog = AlertDialog.Builder(this,R.style.videodiscussdialog)
            .setView(getView(id,isVideo,listener))
            .create()
    val window = dialog?.window
    window?.setGravity(Gravity.BOTTOM)

    window?.decorView?.setPadding(0, 0, 0, 0)
    val lp: WindowManager.LayoutParams? = window?.attributes
    lp?.width = WindowManager.LayoutParams.FILL_PARENT
    lp?.height = WindowManager.LayoutParams.WRAP_CONTENT
    window?.attributes = lp

    dialog?.show()
}

private fun Context.getView(id: String,isVideo:Boolean,listener:()->Unit): View {
    val view = View.inflate(this, R.layout.dialog_video_discuss_add, null)
    val commit = view.findViewById<TextView>(R.id.btCommit)
    val close = view.findViewById<ImageView>(R.id.ivClose)
    val rating = view.findViewById<RatingBar>(R.id.rbStar)
    val tv2 = view.findViewById<TextView>(R.id.tv2)
    rating.setVisibility(isVideo)
    tv2.setVisibility(isVideo)
    close.setOnClickListener {
        dialog?.dismiss()
    }
    commit.setOnClickListener {
        val rating1 = rating.rating
        val content = view.findViewById<EditText>(R.id.etContent).text.toString()
        if (content.isNotEmpty()){
            if (isVideo){
                VideoRequest.videoDiscussAdd(id,content,rating1){
                    listener()
                    dialog?.dismiss()
                }
            }else{
                VideoRequest.gameDiscussAdd(content){
                    listener()
                    dialog?.dismiss()
                }
            }
        }else{
            getString(R.string.pleaseInputDiscuss).toast()
        }
    }
    return view
}
